import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter/material.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/constant/type.dart';
import 'package:hm_show_flutter/entity/home_entity.dart';
import 'package:hm_show_flutter/style/style.dart';
import 'package:hm_show_flutter/utils/navigator_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/utils/shared_preferences_util.dart';
import 'package:hm_show_flutter/widgets/cached_image.dart';

///轮播图
class SwiperView extends StatelessWidget {
  final List<HomeDataCarousellist> bannerData;
  final double viewHeight;

  SwiperView(this.bannerData, this.viewHeight);

  @override
  Widget build(BuildContext context) {
    _goCategoryView(BuildContext context, HomeDataCarousellist channel) {
      switch (channel.type) {
        case JumpType.GOODS_LIST:
          NavigatorUtils.goGoodsListPage(context);
          break;
        case JumpType.ACTIVITY_LIST:
          NavigatorUtils.goActivityListPage(context);
          break;
        case JumpType.RECOMMEND_LIST:
          NavigatorUtils.goCompanyListPage(context, CompanyType.RECOMMEND_LIST);
          break;
        case JumpType.BRAND_LIST:
          NavigatorUtils.goCompanyListPage(context, CompanyType.BRAND_LIST);
          break;
        case JumpType.STORE_INFO:
          SharedPreferencesUtils.getCompany().then((company) {
            if (company != null) {
              NavigatorUtils.goCompanyDetailPage(context, company.id);
            }
          });
          break;
        case JumpType.WEB_URL:
          NavigatorUtils.goWebView(context, null, channel.url);
          break;
      }
    }

    return Container(
      height: viewHeight,
      width: double.infinity,
      child: bannerData == null || bannerData.length == 0
          ? Container(
              height: ScreenUtil.instance.setHeight(400.0),
              color: Colors.grey[100],
              alignment: Alignment.center,
              child: Text(Strings.NO_DATA_TEXT),
            )
          : Swiper(
              onTap: (index) {
                _goCategoryView(context, bannerData[index]);
              },
              itemCount: bannerData.length,
              scrollDirection: Axis.horizontal,
              //滚动方向，设置为Axis.vertical如果需要垂直滚动
              loop: true,
              //无限轮播模式开关
              index: 0,
              //初始的时候下标位置
              autoplay: true,
              autoplayDelay: 3000,
              itemBuilder: (BuildContext buildContext, int index) {
                return CachedImageView(
                    double.infinity, double.infinity, bannerData[index].url);
              },
              duration: 500,
              pagination: SwiperPagination(
                  alignment: Alignment.bottomCenter,
                  builder: DotSwiperPaginationBuilder(
                      size: 8.0,
                      color: Colors.white,
                      activeColor: AppColors.mainColor)),
            ),
    );
  }
}
